<?php
include '../template.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include 'peptideformat.php';
include '../util/pageutil.php';
getTopWithJS("..");
?>

	
<?php
//if(!isset($_POST['search'])&&!isset($_GET['page'])) {
//	die('Please input residues!');
//}
//
//if (isset($_POST['search'])) { // first
//	$seq = trim($_POST['seq']);
//	$org = $_POST['org'];
//        $ref = $_POST[ref];
//
//	// by default we show first page
//	$pageNum = 1;
//} else { //if(isset($_GET['page']))
	$pageNum = $_GET['page'];
        if (!isset($pageNum))
                $pageNum = 1;
	$seq = $_GET['seq'];
	$org = $_GET['org'];
        $ref = $_GET['ref'];
        $count = $_GET['count'];
//}

if (strlen($seq)==0) {
	echo "&nbsp;<br /> &nbsp;<br />";
	echo "Please search by peptide residure sequence!";
} else {

	$where = "lower(peptide.sequence) LIKE '%$seq%'";
	$get = "seq=$seq";
	
	if (!empty($org)) { // some specific org was selected
			$where = $where." AND '$org'=(SELECT organism FROM protein WHERE protein.id=peptide.protein)";
			$get = $get."&org=$org";
	} 
        	
	if (!empty($ref)) { // some specific ref was selected
            $where = $where." AND '$ref'=(SELECT dataSource FROM mass WHERE mass.id=peptide.mass)";
            $get = $get."&ref=$ref";
	} 

	if (strlen($where)!=0) {

		// how many rows to show per page
		$rowsPerPage = 20;

		// counting the offset
		$offset = ($pageNum - 1) * $rowsPerPage;
	
                if (!isset($count)) {
                    // get count
                    $query  = "SELECT COUNT(DISTINCT phosphoSequence,location,protein) ".
                          "FROM peptide WHERE $where;";
                    $result = mysql_query($query) or die("Error, query failed --$query");
                    while ($row = mysql_fetch_array($result)) {
                            $count = $row[0];
                    }
                }
                $get .= "&count=$count";
		echo "<p><font size=\"4\">Totally found $count phosphopeptides:</font></p>";
		
		// query database
		$query  = "SELECT DISTINCT phosphoSequence,location,protein.id,protein.annotation,organism.latin ".
                          "FROM peptide,protein,organism WHERE $where AND peptide.protein=protein.id AND protein.organism=organism.label ".
                          "LIMIT $offset, $rowsPerPage;";
		$result = mysql_query($query) or die("Error, query failed--$query");
		
		// write the data
		echo "<table border=\"1\">";
		echo "<tr><td><b>Peptide<b></td>".
                         "<td><b>Protein</b></td>".
                         "<td><b>Organism<b></td>".
                         "</tr>";
		while($row = mysql_fetch_array($result, MYSQL_ASSOC))
		{
                        $pnrseq = $row['phosphoSequence'];
                        $linkformat = str_replace('#', '%23', $pnrseq);
                        $formated = format_nr_pep_colored_sites($pnrseq);
                        $protId = $row['id'];
                        $loc = $row['location'];
			echo "<tr><td><a href=\"../data/peptide.php?pnrseq=$linkformat&pro=$protId&loc=$loc\">$formated</a> </td>";
			echo "<td><a href=\"../data/protein.php?id={$protId}";
                        if (!empty($ref))
                            echo "&ref=$ref";
                        echo "\">".$row['annotation']."</a></td>";
                        
                        echo "<td><i>{$row['latin']}</i></td>";
                        echo "</tr>";
		}
		echo "</table>";

		// how many pages we have when using paging?
		$maxPage = ceil($count/$rowsPerPage);
		$self = $_SERVER['PHP_SELF'];
                printPageNav($maxPage,"$self?$get&",$pageNum);

//		if ($maxPage>1) {
//		// print the link to access each page
//		$nav = '';
//                $pagestart = $pageNum-7;
//                if ($pagestart<1)
//                    $pagestart = 1;
//                $pageend = $pageNum+7;
//                if ($pageend>$maxPage)
//                    $pageend = $maxPage;
//		for($page = $pagestart; $page <= $pageend; $page++)
//		{
//			if ($page == $pageNum)
//			{
//				$nav .= " $page ";   // no need to create a link to current page
//			}
//			else
//			{
//				$nav .= " <a href=\"$self?page=$page&$get\">$page</a> ";
//			}
//		}
//
//		// creating previous and next link
//		// plus the link to go straight to
//		// the first and last page
//
//		if ($pageNum > 1)
//		{
//			$page = $pageNum - 1;
//			$prev = " <a href=\"$self?page=$page&$get\">[Prev]</a> ";
//
//			$first = " <a href=\"$self?page=1&$get\">1</a> ";
//		}
//		else
//		{
//			$prev  = '&nbsp;'; // we're on page one, don't print previous link
//			$first = '&nbsp;'; // nor the first page link
//		}
//
//		if ($pageNum < $maxPage)
//		{
//			$page = $pageNum + 1;
//			$next = " <a href=\"$self?page=$page&$get\">[Next]</a> ";
//
//			$last = " <a href=\"$self?page=$maxPage&$get\">$maxPage</a> ";
//		}
//		else
//		{
//			$next = '&nbsp;'; // we're on the last page, don't print next link
//			$last = '&nbsp;'; // nor the last page link
//		}
//
//		// print the navigation link
//		echo $prev . $first . $nav . $last . $next;
//		} // end if ($maxPage>1)
		
		include '../dbconfig/closedb.php';
	}
}
?>

	

<?php getBottom(); ?>
